package com.xb.system.controller;



import com.xb.common.result.Result;
import com.xb.model.system.SysMenu;
import com.xb.model.vo.AssginMenuVo;
import com.xb.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author xb
 * @since 2023-08-23
 */
//@Api(tags = "菜单管理") //Swagger生成api的提示
//@RestController //ResponseBody+Controller ResponseBody:将数据以JSON的格式返回
//@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;
    /**
     *根据id删除
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        boolean flag = sysMenuService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }



    /**
     * 添加
     * RequestBody 不用于get请求中
     * 将请求体的内容的JSON数据封装到实体类中,可以省略
     *
     * @param sysMenu
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation(value = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu) {
        boolean flag = sysMenuService.save(sysMenu);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysMenu menu = sysMenuService.getById(id);
        return Result.ok(menu);
    }

    /**
     * 根据id修改
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation(value = "根据id修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysMenu menu) {
        boolean flag = sysMenuService.updateById(menu);
        return flag ? Result.ok() : Result.fail() ;
    }

    /**
     * 菜单树
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation(value = "获取菜单")
    @GetMapping("nodes")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    /**
     * 根据用户获取菜单
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/menus/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    /**
     * 给角色分配菜单
     * @param assginMenuVo
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/assign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }
}

